package com.hy.study.driver;

import com.hy.study.mapper.KeyValueMapper;
import com.hy.study.reduce.KeyValueReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 01 月 31 日 9:48: 51
 */
public class KeyValueDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        final String inputfile = "F:\\study\\Hadoop\\input\\phone_data\\phone_data.txt";
        final String outputfile = "F:\\study\\Hadoop\\output\\phone_result2";
        Configuration conf = new Configuration();
        // 设置key value 切割方式的切割字段 默认是\t
        conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, "\t");
        Job job = Job.getInstance(conf);
        job.setInputFormatClass(KeyValueTextInputFormat.class);

        job.setJarByClass(KeyValueDriver.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.setMapperClass(KeyValueMapper.class);
        job.setReducerClass(KeyValueReducer.class);

        FileInputFormat.addInputPath(job, new Path(inputfile));
        FileOutputFormat.setOutputPath(job, new Path(outputfile));

        boolean completion = job.waitForCompletion(true);

        System.exit(completion ? 0 : 1);

    }
}
